package com.lry.basic.algorithm.common;

/**
 * @author:刘仁有
 * @desc:
 * @email:953506233@qq.com
 * @data:2019/11/26
 */
public class MinSubArrayLen {
    public static void main(String[] args) {
        MinSubArrayLen msal = new MinSubArrayLen();
        System.out.println(msal.minSubArrayLen(7, new int[]{2,3,1,2,4,3}));
        System.out.println(msal.minSub(7, new int[]{2,3,1,2,4,3}));
    }

    public int minSub(int s,int []nums){
        int min = Integer.MAX_VALUE;
        int left = 0;
        int sum = 0;

        for(int i=0;i<nums.length;i++){
            sum+=nums[i];
            while(sum>=s){
                min = Math.min(min,i+1-left);
                sum -= nums[left++];
            }
        }

        return min==Integer.MAX_VALUE?0:min;
    }

    public int minSubArrayLen(int s, int[] nums){
        if(null==nums||nums.length==0)
            return 0;
        int min = Integer.MAX_VALUE;
        int i = 0;
        while(i<nums.length){
            int sum = 0;
            int j = i;
            int num = 0;
            while(j<nums.length&&sum<s){
                sum+=nums[j];
                j++;
                num++;
            }
            if(min>num&&sum>=s){
                min = num;
            }
            sum = 0;
            num = 0;
            i++;
        }
        return min==Integer.MAX_VALUE?0:min;
    }



}

